﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace SilverlightControls.Designer
{
    public class Toolbox : ContentControl
    {
        ScrollViewer _layoutRoot;
        Grid _mainGrid;
        static readonly Type _thisType = typeof(Toolbox);
        private FormDesigner _designer;
        public FormDesigner Designer { get; set; }

        #region Toolbox BorderBrush

        public static readonly DependencyProperty ToolboxBorderBrushProperty =
            DependencyProperty.Register("ToolboxBorderBrush", typeof(Brush), _thisType, new PropertyMetadata(new SolidColorBrush(Colors.LightGray), OnToolboxBorderBrushChanged));

		/// <summary>
		/// Gets or sets the Border Brush of the Property Grid
		/// </summary>
        public Brush ToolboxBorderBrush
		{
            get { return (Brush)GetValue(ToolboxBorderBrushProperty); }
            set { SetValue(ToolboxBorderBrushProperty, value); }
		}

        private static void OnToolboxBorderBrushChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
            var toolbox = d as Toolbox;
            if (toolbox != null && null != toolbox._layoutRoot && null != e.NewValue)
                toolbox._layoutRoot.BorderBrush = (SolidColorBrush)e.NewValue;
		}

		#endregion

        #region Toolbox BorderThickness

        public static readonly DependencyProperty ToolboxBorderThicknessProperty =
            DependencyProperty.Register("ToolboxBorderThickness", typeof(Thickness), _thisType, new PropertyMetadata(new Thickness(1), OnToolboxBorderThicknessChanged));

		/// <summary>
		/// Gets or sets the Border Thickness of the Property Grid
		/// </summary>
        public Thickness ToolboxBorderThickness
		{
            get { return (Thickness)base.GetValue(ToolboxBorderThicknessProperty); }
            set { base.SetValue(ToolboxBorderThicknessProperty, value); }
		}


        private static void OnToolboxBorderThicknessChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var toolbox = d as Toolbox;
            if (toolbox != null && null != toolbox._layoutRoot && null != e.NewValue)
                toolbox._layoutRoot.BorderThickness = (Thickness)e.NewValue;
		}

		#endregion

        public Toolbox()
        {
            DefaultStyleKey = typeof(Toolbox);
            Resources.Source = new Uri("/SilverlightControls;component/Designer/Themes/Generic.xaml", UriKind.Relative);
        }

        #region Overrides
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            _layoutRoot = (ScrollViewer)GetTemplateChild("LayoutRoot");
            _mainGrid = (Grid)GetTemplateChild("MainGrid");
            AddElement(new TextBox { Text = "TextBox"});
            AddElement(new Border { Width = 100, Height = 70 });
            AddElement(new Button { Content = "Button" });
            AddElement(new CheckBox { Content = "CheckBox" });
            AddElement(new ComboBox { SelectedValue = "ComboBox" });
            AddElement(new Grid { Width = 100, Height = 70 });
            AddElement(new Image { Width = 100, Height = 70 });
            AddElement(new AutoCompleteBox { Text = "AutoCompleteBox" });
            AddElement(new Calendar { SelectedDate = DateTime.Now });
            AddElement(new Canvas { Width = 100, Height = 70 });
            AddElement(new ContentControl { Width = 200, Height = 200 });
            AddElement(new DatePicker { SelectedDate = DateTime.Now });
            AddElement(new Frame { Width = 100, Height = 70 });
            AddElement(new GridSplitter());
            AddElement(new HyperlinkButton { Content = "HyperlinkButton" });
            AddElement(new PasswordBox());
            AddElement(new ProgressBar());
        }

        public void AddElement(FrameworkElement element)
        {
            if (double.IsNaN(element.Height))
                element.Height = 22;
            if (double.IsNaN(element.Width))
                element.Width = 55;
            var fi = new FormItem(element);
            var fe = new ToolboxItem(this, fi);

            _mainGrid.RowDefinitions.Add(new RowDefinition());
            _mainGrid.Children.Add(fe);
            Grid.SetRow(fe, _mainGrid.RowDefinitions.Count - 1);
        }

        #endregion
    }
}
